library(stargazer)
library(survey)
library(foreign)
setwd("~/Replication Code/")

##---Loading in data ----
anes9297 <- read.dta("./data/anes9297_subset.dta")

ccap08 <- read.dta("./data/ccap08_subset.dta")

ccap12 <- read.dta("./data/ccap12_subset.dta")

vsg.dat <- read.dta("./data/vsg_subset.dta")

ccap16 <- read.dta("./data/ccap16_subset.dta")

# Table 1 Entries-----
## ANES
# Dem 
party <- -1
x <- subset(anes9297, pid92_3 == party & pid94_3 == party
            & !is.na(rr94_scaled) & !is.na(rr92_scaled))
d <- svydesign(~1, data = x, weights = ~V940005)
svymean(~rr92_scaled, d, na.rm = T)
svymean(~rr94_scaled, d, na.rm = T)

# Rep 
party <- 1
x <- subset(anes9297, pid92_3 == party & pid94_3 == party
            & !is.na(rr94_scaled) & !is.na(rr92_scaled))
d <- svydesign(~1, data = x, weights = ~V940005)
svymean(~rr92_scaled, d, na.rm = T)
svymean(~rr94_scaled, d, na.rm = T)

## CCAP08
# Dem
party <- -1
x <- subset(ccap08, pid3_m == party & pid3_o == party
            & !is.na(rr_m_scaled) & !is.na(rr_o_scaled))
d <- svydesign(~1, data = x, weights = ~weight)
svymean(~rr_m_scaled, d, na.rm = T)
svymean(~rr_o_scaled, d, na.rm = T)

# Rep 
party <- 1
x <- subset(ccap08, pid3_m == party & pid3_o == party
            & !is.na(rr_m_scaled) & !is.na(rr_o_scaled))
d <- svydesign(~1, data = x, weights = ~weight)
svymean(~rr_m_scaled, d, na.rm = T)
svymean(~rr_o_scaled, d, na.rm = T)

## CCAP12 MARCH
# Dem
party <- -1
x <- subset(ccap12, p2 == 1  & pp_pid3_new == party & pid3_new == party 
            & !is.na(rr_p1_scaled) & !is.na(rr_p2_scaled))
d <- svydesign(~1, data = x, weights = ~weight)
svymean(~rr_p1_scaled, d, na.rm = T)
svymean(~rr_p2_scaled, d, na.rm = T)
# Rep
party <- 1
x <- subset(ccap12, p2 == 1  & pp_pid3_new == party & pid3_new == party 
            & !is.na(rr_p1_scaled) & !is.na(rr_p2_scaled))
d <- svydesign(~1, data = x, weights = ~weight)
svymean(~rr_p1_scaled, d, na.rm = T)
svymean(~rr_p2_scaled, d, na.rm = T)

## CCAP12 AUGUST
# Dem
party <- -1
x <- subset(ccap12, p3 == 1  & pp_pid3_new == party & pid3_new == party 
            & !is.na(rr_p1_scaled) & !is.na(rr_p3_scaled))
d <- svydesign(~1, data = x, weights = ~weight)
svymean(~rr_p1_scaled, d, na.rm = T)
svymean(~rr_p3_scaled, d, na.rm = T)

# Rep
party <- 1
x <- subset(ccap12, p3 == 1  & pp_pid3_new == party & pid3_new == party 
            & !is.na(rr_p1_scaled) & !is.na(rr_p3_scaled))
d <- svydesign(~1, data = x, weights = ~weight)
svymean(~rr_p1_scaled, d, na.rm = T)
svymean(~rr_p3_scaled, d, na.rm = T)

## VSG
# Dem
party <- -1
x <- subset(vsg.dat, pid3_12 == party & pid3_16 == party
            & !is.na(rr_sc_12) & !is.na(rr_sc_16))
d <- svydesign(~1, data = x, weights = ~weight)
svymean(~rr_sc_12, d, na.rm = T)
svymean(~rr_sc_16, d, na.rm = T)

# Rep
party <- 1
x <- subset(vsg.dat, pid3_12 == party & pid3_16 == party
            & !is.na(rr_sc_12) & !is.na(rr_sc_16))
d <- svydesign(~1, data = x, weights = ~weight)
svymean(~rr_sc_12, d, na.rm = T)
svymean(~rr_sc_16, d, na.rm = T)

## CCAP16
# Dem
party <- -1
x <- subset(ccap16, b_pid3 == party & p_pid3 == party
            & !is.na(b_rr_sc) & !is.na(p_rr_sc))
d <- svydesign(~1, data = x, weights = ~weight_post)
svymean(~b_rr_sc, d, na.rm = T)
svymean(~p_rr_sc, d, na.rm = T)

# Rep
party <- 1
x <- subset(ccap16, b_pid3 == party & p_pid3 == party
            & !is.na(b_rr_sc) & !is.na(p_rr_sc))
d <- svydesign(~1, data = x, weights = ~weight_post)
svymean(~b_rr_sc, d, na.rm = T)
svymean(~p_rr_sc, d, na.rm = T)

#----------------------------------------------------------------------------------------------------#
# ANES MODELS  -- 92-94
#----------------------------------------------------------------------------------------------------#
dat1 <- na.omit(anes9297[c("white_int92", "white_int94",
                            "pid92_3", "pid94_7scaled",
                            "rr92_scaled", "rr94_scaled", "V940005")])


### PID on RR
m1 <- lm(rr94_scaled ~  relevel(as.factor(pid92_3), 2) + rr92_scaled, 
         data = dat1, weights = V940005, 
         subset = white_int92 == white_int94)
summary(m1)
summary(update(m1, ~ as.factor(pid92_3)  + rr92_scaled))

#----------------------------------------------------------------------------------------------------#
# CCAP 2008
#----------------------------------------------------------------------------------------------------#
dat2 <- na.omit(ccap08[c("pid3_m", "rr_m_scaled", "rr_o_scaled", 
                          "pid7r_o_sc", "weight")]) 

### PID on RR
m2 <- lm(rr_o_scaled ~ relevel(as.factor(pid3_m), 2)  + rr_m_scaled, data = dat2, weights = weight)
summary(m2)
summary(update(m2, ~ as.factor(pid3_m)  + rr_m_scaled))

#----------------------------------------------------------------------------------------------------#
# CCAP 2012: P2: December to ~ March 24 - 30 per CCAP website
#----------------------------------------------------------------------------------------------------#
ccap.p2 <- subset(ccap12, p2 == 1, 
                  select = c("rr_p1_scaled", "rr_p2_scaled", "pp_pid3_new", "pid7_sc", "weight"))
dat3 <- na.omit(ccap.p2)

# PID on RR
m3 <- lm(rr_p2_scaled ~  relevel(as.factor(pp_pid3_new), 2)  + rr_p1_scaled, data = dat3, weights = weight)
summary(m3)
summary(update(m3, ~ as.factor(pp_pid3_new) + rr_p1_scaled))

#----------------------------------------------------------------------------------------------------#
# CCAP 2012: P3: December to ~August 25-31 -- RNC convention per CCAP website
#----------------------------------------------------------------------------------------------------#
ccap.p3 <- subset(ccap12, p3 == 1, 
                  select = c("rr_p1_scaled", "rr_p3_scaled", "pp_pid3_new", "pid7_sc", "weight"))
dat4 <- na.omit(ccap.p3)

# PID on RR
m4 <- lm(rr_p3_scaled ~  relevel(as.factor(pp_pid3_new), 2)  + rr_p1_scaled, data = dat4, weights = weight)
summary(m4)
summary(update(m4, ~ as.factor(pp_pid3_new) + rr_p1_scaled))

#----------------------------------------------------------------------------------------------------#
# VSG 2012-2016
#----------------------------------------------------------------------------------------------------#
dat5 <- na.omit(vsg.dat[c("rr_sc_12", "rr_sc_16", "pid3_12", "pid7_sc_16", "weight")])

# PID on RR
m5 <- lm(rr_sc_16 ~ relevel(as.factor(pid3_12), 2) + rr_sc_12, 
         data = dat5, weights = weight)
summary(m5)
summary(update(m5, ~ as.factor(pid3_12) + rr_sc_12))

#----------------------------------------------------------------------------------------------------#
# CCAP 2016
#----------------------------------------------------------------------------------------------------#
dat6 <- na.omit(ccap16[c("b_rr_sc", "p_rr_sc", "b_pid3", "p_pid7_sc", "weight_post")])


# PID on RR
m6 <- lm(p_rr_sc ~ relevel(as.factor(b_pid3), 2) + b_rr_sc , 
         data = dat6, weights = weight_post)
summary(m6)
summary(update(m6, ~ as.factor(b_pid3) + b_rr_sc))

#----------------------------------------------------------------------------------------------------#
# Table 2----
m1_c <- m1
m2_c <- m2
m3_c <- m3
m4_c <- m4
m5_c <- m5
m6_c <- m6

names(m1_c$coefficients) <- names(coef(m1_c))
names(m2_c$coefficients) <- names(coef(m1_c))
names(m3_c$coefficients) <- names(coef(m1_c))
names(m4_c$coefficients) <- names(coef(m1_c))
names(m5_c$coefficients) <- names(coef(m1_c))
names(m6_c$coefficients) <- names(coef(m2_c))


stargazer(m1_c, m2_c, m3_c, m4_c, m5_c, m6_c,
          title = "Relationship between Partisanship and Racial Resentment",
          covariate.labels = c("Democrat$_{t-1}$", "Republican$_{t-1}$", "Racial Resentment$_{t-1}$"),
          no.space = T, notes = c("OLS regression results. Standard errors in parentheses. Pure Independents are the reference category."), 
          dep.var.caption = "", model.numbers = F,
          dep.var.labels = c("Racial Resentment"),
          column.labels = c("ANES 1992-1994", "CCAP 2008", "CCAP2012: March", "CCAP2012: August", "VOTER Survey", "CCAP 2016"),
          table.placement = "t",
          notes.align = "l", intercept.bottom = T, 
          digits = 3, df = F, omit.stat = c("f", "adj.rsq"), align = T, star.char = c("*"), star.cutoffs = c(0.05)
)


